import kohutils
class Player():
    def __init__(self, number):
        self.number = number
        self.face_down_cards = []
        self.face_up_cards = []
        self.hand = []

    def play_card(self, card_group, cards_to_play):
        # Plays a card.
        # card_group: 0 = hand, 1 = cards on field 
        # cards_to_play = which cards

        cards_played = []
        
        for card_index in cards_to_play:
            if card_group == 0:
                if self.hand and len(self.hand) >= card_index:
                    card = self.hand.pop(card_index)
                    cards_played.append(card)
                    
            elif card_group == 1:
                if not self.hand:
                    if self.face_up_cards[card_index] == None:
                        if self.face_down_cards != None:
                            card = self.face_down_cards[card_index]
                            self.face_down_cards[card_index] = None
                            cards_played.append(card)
                    else:
                        card = self.face_up_cards[card_index]
                        self.face_up_cards[card_index] = None
                        cards_played.append(card)

        return cards_played

    def get_face_up_cards(self):
        return self.face_up_cards
    
    def get_face_down_cards(self):
        return self.face_down_cards

    def show_face_up_cards(self):
        index = 0
        
        face_up_cards = self.face_up_cards
        print("Face-up Cards: ", end = ' ')          
        for card in face_up_cards:
            index += 1
            print(index, end = ' ')
            if card:
                for element in card:
                    print(element, end = '')
            else:
                print(card, end = '')
            print(end = '\t')
        index = 0
        print()

    def show_hand(self, setup=False):
        index = 0
        
        hand = self.hand
        print("Hand:", end = ' ')
        for card in hand:
            index += 1
            if setup:
                print(index, end = ' ')
            for element in card:
                print(element, end = '')
            print(end = '\t')
        index = 0
        print()
                
    def hand_length(self):
        return len(self.hand)

    def hand_count(self, card_req):
        card_count = 0
        for card in self.hand:
            if card_req[1] == card[1]:
                card_count += 1
                
        return card_count
    
    def sort_hand(self):
        self.hand = kohutils.sort_cards(self.hand)

    def switch_face_up_card(self, face_up_card, hand_card):
        if not kohutils.restrict_input(face_up_card, 0, 2):
            return False
        elif not kohutils.restrict_input(hand_card, 0, 4):
            return False
        
        tmp_card = self.face_up_cards[face_up_card]
        self.face_up_cards[face_up_card] = self.hand[hand_card]
        self.hand[hand_card] = tmp_card
        return True
    
